/*************************************************************************
 * Copyright 2008 David Almilli
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *      
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 **************************************************************************/
package com.almilli.catnip.server;

import com.almilli.catnip.beacon.TiVoBeacon;
import com.almilli.catnip.logging.Log;
import com.almilli.catnip.logging.LogFactory;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.catalina.Container;
import org.apache.catalina.Engine;
import org.apache.catalina.Server;
import org.apache.catalina.Service;
import org.apache.catalina.deploy.ContextResource;
import org.apache.catalina.deploy.NamingResources;

/**
 * The <code>ContextUtils</code> class provides useful utility methods when looking up context
 * information.
 * @author s2kdave
 */
public class ContextUtils {
	private static final Log log = LogFactory.getLog(ContextUtils.class);
	private ContextUtils() {}

	public static Engine getEngine(Container container) {
		while (container != null) {
			if (container instanceof Engine) {
				return (Engine)container;
			}
			container = container.getParent();
		}
		return null;
	}

	public static Server getServer(Container container) {
		Engine engine = getEngine(container);
		if (engine != null) {
			Service service = engine.getService();
			if (service != null) {
				return service.getServer();
			}
		}
		return null;
	}

	public static String getTiVoBeaconName(Container container) {
		if (container instanceof Server) {
			return getTiVoBeaconName((Server)container);
		} else {
			return getTiVoBeaconName(getServer(container));
		}
	}
	
	public static String getTiVoBeaconName(Server server) {
		if (server != null) {
			NamingResources naming = server.getGlobalNamingResources();
			ContextResource[] all = naming.findResources();
			for (ContextResource res : all) {
				if (res.getType().equals(TiVoBeacon.class.getName())) {
			        return res.getName();
				}
			}
		}
		return null;
	}

	public static TiVoBeacon getTiVoBeacon(Container container) {
		if (container instanceof Server) {
			return getTiVoBeacon((Server)container);
		} else {
			return getTiVoBeacon(getServer(container));
		}
	}
	
	public static TiVoBeacon getTiVoBeacon(Server server) {
		String name = getTiVoBeaconName(server);
		if (name != null) {
	        try {
	        	//lookup the resource
	            return (TiVoBeacon)new InitialContext().lookup("java:" + name);
	        } catch (NamingException e) {
	            if (log.isWarnEnabled()) {
	                log.warn("Cannot find the TiVoBeacon in JNDI.  It MUST be " +
	                    "defined in the GlobalNamingResources section.", e);
	            }
	        }
		}
		return null;
	}
}
